//
//  AppDelegate.swift
//  KySwiftPro
//
//  Created by fangyukui on 2017/10/17.
//  Copyright © 2017年 fangyukui. All rights reserved.
//

import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        window = UIWindow()
        window?.rootViewController = KyMainViewController()
        window?.makeKeyAndVisible()
        
        loadAppData()
        register()
        
        return true
    }
    

}

extension AppDelegate{
    //模拟从网络异步加载数据
    fileprivate func loadAppData(){
        
        DispatchQueue.global().async {
            
            guard let url =  Bundle.main.url(forResource: "MainVCSettings.json", withExtension: nil) else
            {
                KyLog(message: "加载app数据错误")
                return
            }
            do {
                let data = try Data(contentsOf: url)
                var  localPath =  NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
                localPath = (localPath as NSString).appendingPathComponent("main.json")
                let fileUrl = URL(fileURLWithPath: localPath)
                let _ = try data.write(to: fileUrl)
//                KyLog(message: fileUrl)
                
            } catch let error  {
                KyLog(message: error)
            }

            
        }
        
    }
    
    fileprivate func register(){
        
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge,.carPlay]) { (sucess, fail) in
                KyLog(message: "授权\(sucess)" )
            }
        } else {
            let setting = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(setting)
        }
        
        
       
        
       
        
    }
    
}

